<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;
use app\admin\model\PosterSpace;
use app\admin\model\Poster as PosterModel;

class Poster extends Admin {

    public $posterSpace;
    public $poster;

    public function __construct(\think\Request $request = null) {
        parent::__construct($request);
        $this->posterSpace = new PosterSpace();
        $this->poster = new PosterModel();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index() {
        $list = $this->posterSpace->select();
        // var_dump($list);
        $this->assign('list', $list);
        $this->assign('posterStatus', config('poster_space_status'));
        $this->assign('posterType', config('poster_type'));
        return $this->fetch();
    }

    /**
     * 添加/编辑广告位
     * @return \think\Response
     */
    public function editspace() {
        $data = $this->request->param();
        if ($data['id']) {
            $this->posterSpace->where('id', $data['id'])->update($data);
        } else {
            $this->posterSpace->insert($data);
        }
        $this->success('操作成功');
    }

    /**
     * 删除广告位
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delspace($id) {
        $this->posterSpace->where('id', $id)->delete();
        $this->poster->where('spaceid', $id)->delete();
        $this->success('删除成功');
    }

    /**
     * 广告位内容列表
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function posterlist(Request $request, $id) {

        $list = $this->poster->where('spaceid', $id)->order('list_order')->select();
        $space = $this->posterSpace->find($id);

        $this->assign('posterStatus', config('poster_space_status'));
        $this->assign('posterType', config('poster_type'));
        $this->assign('list', $list);
        $this->assign('space', $space);
        return $this->fetch();
        //var_dump($list);
    }

    /**
     * 更新排序
     */
    public function setOrder() {
        $param = $this->request->param();

        $this->_setorder($param['list_order'], 'poster');

        $this->success('排序成功');
    }

    /**
     * 添加编辑广告内容
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function editposter(Request $request) {
        $param = $this->request->only(['id', 'name', 'spaceid', 'type', 'subtitle', 'description', 'setting', 'startdate', 'enddate', 'link', 'link_title', 'img_path', 'addtime', 'hits', 'clicks', 'list_order', 'status']);

        if ($this->request->isPost()) {
            if ($param['startdate']) {
                $param['startdate'] = strtotime($param['startdate']);
            }
            if ($param['enddate']) {
                $param['enddate'] = strtotime($param['enddate']);
            }
            if ($param['id']) {
                $this->poster->where('id', $param['id'])->update($param);
            } else {
                $param['addtime'] = time();
                $this->poster->insert($param);
            }

            $this->success("操作成功，正返回广告列表", url('poster/posterlist', ['id' => $param['spaceid']]));
            exit;
        }
        if (isset($param['id'])) {
            $poster = $this->poster->find($param['id']);
            $this->assign('info', $poster);
        }

        $space = $this->posterSpace->find($param['spaceid']);

        $this->assign('posterStatus', config('poster_space_status'));
        $this->assign('posterType', config('poster_type'));
        $this->assign('space', $space);
        return $this->fetch();
    }

    /**
     * 删除广告内容
     * @param type $id
     */
    public function delPoster($id) {
        $this->poster->where('id', $id)->delete();
        $this->success('删除成功');
    }

}
